package com.chpro.scsw.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.chpro.common.HttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;

import com.chpro.scsw.dao.IArticleDao;
import com.chpro.scsw.dao.IPcTitleDao;
import com.chpro.scsw.model.ArticleModel;
import com.chpro.scsw.model.PcTitle;

/**
 * 财富江湖实现类
 * @author Administrator
 *
 */
@Repository("cfjhArticleDaoImpl")
public class CfjhArticleDaoImpl extends BaseDaoImpl<ArticleModel> {

	public List<ArticleModel> queryInternetArticle()throws Exception {
		CfjhArticleDaoImpl cfjhArticleDaoImpl = new CfjhArticleDaoImpl();
		
		//首先获取文章列表
//		List<Map<String,String>> articleList = cfjhArticleDaoImpl.queryTitleAndUrl();
//		for(Map<String,String> map:articleList){
//			System.out.println(map);
//		}
		
		
		return null;
				
		
	}

	/**
	 * 获得页数
	 * @return
	 */
	public Integer queryPageCount(){
		String uri = "http://caifujianghu.com/new/all";
		String html = HttpUtil.httpGet(uri);	//获取文章列表页面
		Document doc = Jsoup.parse(html);//解析HTML字符串返回一个Document实现 
		String a = doc.select(".btn-group>.btn").last().attr("href");
		return Integer.parseInt(a.substring(a.lastIndexOf("/")+1));
	}
	
	/**
	 * 首先获取文章列表
	 * @return
	 */
	public List<Map<String,String>> queryTitleAndUrl(){
		int lastIndex = queryPageCount();
		
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();

		String uri = "http://caifujianghu.com/new/all/";
		
		for(int i=1;i<=lastIndex;i++){
			String html = HttpUtil.httpGet(uri+i);	//获取文章列表页面
	
			Document doc = Jsoup.parse(html);//解析HTML字符串返回一个Document实现 
			Elements links = doc.select(".content");
			for(Element link:links){
				Map temp = new HashMap<String,String>();
				temp.put("title",link.select("h2>a").eq(0).html());
				temp.put("url",link.select("h2>a").eq(0).attr("href"));
				temp.put("summary",link.select(".desc").html());
				result.add(temp);
				
			}
		}
		return result;
	}
	
	public static void main(String[] args)throws Exception {
		testAdd();
		
	}
	
	public void testAdd1()throws Exception{
		//根据文章列表获取明细
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IPcTitleDao pcTitleDao = ac.getBean("pcTitleDaoImpl",PcTitleDaoImpl.class);
        CfjhArticleDaoImpl article = ac.getBean("cfjhArticleDaoImpl",CfjhArticleDaoImpl.class);
		System.out.println(pcTitleDao);
		List<PcTitle>  PcTitleList = pcTitleDao.queryPcTitleAll();
		
		for(PcTitle pt:PcTitleList){
			String uri = pt.getUrl();
			System.out.println(pt.getTitle());
			String html = HttpUtil.httpGet(uri);	//获取文章列表页面
			Document doc = Jsoup.parse(html);//解析HTML字符串返回一个Document实现 
			
			//得到标题
			String title = doc.select(".content>.text-center").eq(0).html();
			System.out.println("标题:"+title);
			//得到发布时间
			String datetime = doc.select(".content>.aticle-info>.pull-left span").eq(0).html().substring(31);
			System.out.println("发布时间:"+datetime);
			//得到内容
			String content = doc.select(".content p").outerHtml();
			System.out.println("内容:"+content);
			System.out.println("内容长度:"+content.length());
			
			ArticleModel articleModel = new ArticleModel();
			articleModel.setTitle(title);
			articleModel.setReleaseTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(datetime));
			articleModel.setContent(content);
			article.addArticle(articleModel);
			
			System.out.println("===============================================");
			
			
		}
	}
	
	/**
	 * 测试添加
	 */
	public static void testAdd()throws Exception{
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		IPcTitleDao pcTitleDao = ac.getBean("pcTitleDaoImpl",PcTitleDaoImpl.class);
		
		CfjhArticleDaoImpl cad = new CfjhArticleDaoImpl();
		List<Map<String,String>> articleList = cad.queryTitleAndUrl();
		for(Map<String,String> map:articleList){
			System.out.println(map);
			PcTitle pc = new PcTitle();
			pc.setTitle(map.get("title"));
			pc.setUrl(map.get("url"));
			pc.setCreateDate(new Date());
			pc.setSummary(map.get("summary"));
			pcTitleDao.addPcTitle(pc);
		}
	}

	public boolean addArticle(ArticleModel articleModel) throws Exception {
		String classMethod = "com.chpro.scsw.dao.ArticleMapper.addArticleModel";
		return super.add(classMethod, articleModel);
	}

	public List<ArticleModel> queryArticle(int pageNum, int pageSize)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public ArticleModel queryArticleById(String articleId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<ArticleModel> queryArticle(ArticleModel articleModel)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public ArticleModel updateArticleById(String articleId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean updateArticleById(ArticleModel articleModel)
			throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public ArticleModel queryArticleById(ArticleModel articleId)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<ArticleModel> latestArticleAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
